package com.tml.datastructure;

public class TreeNode {

	public int data;
	public TreeNode left,rigth;
	
	public TreeNode(int data) {
		this.data = data;
	}
	
	public int getData() {
		return data;
	}
	public void setData(int data) {
		this.data = data;
	}
	
	/**
	 * 先序遍历
	 * @param treeNode
	 */
	public static void preTraverseBTree(TreeNode treeNode) {
		
		if (treeNode != null) {
			//根
			System.out.print(treeNode.getData()+"->");
			//左
			preTraverseBTree(treeNode.left);
			//右
			preTraverseBTree(treeNode.rigth);
		}
	}
	
	/**
	 * 中序遍历
	 * @param treeNode
	 */
	public static void inTraverseBTree(TreeNode treeNode) {
		if (treeNode != null) {
			//左
			inTraverseBTree(treeNode.left);
			//根
			System.out.print(treeNode.getData()+"->");
			//右
			inTraverseBTree(treeNode.rigth);
		}
	}
	
	/**
	 * 后序遍历
	 * @param treeNode
	 */
	public static void afterTraverseBTree(TreeNode treeNode) {
		if (treeNode != null) {
			//左
			afterTraverseBTree(treeNode.left);
			
			//右
			afterTraverseBTree(treeNode.rigth);
			
			//根
			System.out.print(treeNode.getData()+"->");
			
		}
	}
	public static void main(String[] args) {
		//根节点-->10
        TreeNode treeNode1 = new TreeNode(10);

        //左孩子-->9
        TreeNode treeNode2 = new TreeNode(9);

        //右孩子-->20
        TreeNode treeNode3 = new TreeNode(20);

        //20的左孩子-->15
        TreeNode treeNode4 = new TreeNode(15);

        //20的右孩子-->35
        TreeNode treeNode5 = new TreeNode(35);
        
        treeNode1.left = treeNode2;
        treeNode1.rigth = treeNode3;
        
        treeNode3.left = treeNode4;
        treeNode3.rigth = treeNode5;
        System.out.println("先序遍历:");
        preTraverseBTree(treeNode1);
        System.out.println("中序遍历:");
        inTraverseBTree(treeNode1);
        System.out.println("后序遍历:");
        afterTraverseBTree(treeNode1);
	}
	
}
